<?php
class My_Plugin {

    var $table;
    var $primary;
    var $cols;
    var $prefix;
    var $wertex;
    var $pluginName;
    var $request;
    var $message;
    var $lastId;

    public function  __construct($pluginName, $vertex) {

        $name = explode(' ', str_replace('plugin_', '', $pluginName));
        $this->pluginName = null;
        if(is_array($name)){
            foreach($name AS $word){
                $this->pluginName .= ucfirst($word);
            }
        }else{
            $this->pluginName .= ucfirst($word);
        }

        $this->table = new Zend_Db_Table('plugin_'.$pluginName);

        $this->setWertex($vertex);
        
        $info = $this->table->info();
        $this->primary = $info['primary'][1];
        $this->cols = $info['cols'];

        $this->prefix = 'plugin_'.$pluginName.'_';
        $this->request = new Zend_Controller_Request_Http;
    }

    public function getPathToPlugin(){
        return dirname(__FILE__).DIRECTORY_SEPARATOR.ucfirst($this->getName()).DIRECTORY_SEPARATOR;
    }

    public function getUrlToPlugin(){
        return $this->getUrl().'library/Plugin/'.ucfirst($this->getName()).'/';
    }

    public function getUrl(){
        return 'http://'.$_SERVER['SERVER_NAME'].'/das/';
    }

    public function insert($data){
        $this->lastId = $this->table->insert($data);
        return $this->lastId;
    }

    public function getLastId(){
        return $this->lastId;
    }

    public function getPrefix(){
        return $this->prefix;
    }

    public function setWertex($vertex){
        $this->wertex = $vertex;
    }

    public function getCols(){
        return $this->cols;
    }

    public function getWertex(){
        return $this->wertex;
    }

    public function toTemplate($data, $templateName)
    {
        $filename = dirname(__FILE__).'/'.$this->getName().'/template/'.$templateName.'.html';
        if(!file_exists($filename) OR !is_array($data)){
            return false;
        }
        $content = file_get_contents($filename);
        foreach($data AS $key => $value){
            $content = str_replace('{'.$key.'}', $value, $content);
        }
        return $content;
    }

    public function getMessage()
    {
        return $this->message;
    }

    public function setMessage($message)
    {
        $this->message = $message;
    }

    public function getName(){
        $name = explode('_', $this->pluginName);
        if(is_array($name)){
            foreach ($name as $word) {
                $plugin .= ucfirst($word);
            }
        }else{
            $plugin .= ucfirst($word);
        }

        return $plugin;
    }

    public function getHeader(){
        return $this->getName();
    }

    public function getLastRow(){
        $result = $this->table->fetchAll(
            $this->table->select()
                ->where('wertex = ?', $this->getWertex())
                ->order('id DESC')
            );
        $rows = $result->toArray();
        if(count($rows)){
            return $rows[0];
        }
        return false;
    }

    public function getTable(){
        return $this->table;
    }

    public function isVisual(){
        return true;
    }
}
?>
